<?php
class CMenuLinkTree extends CWidget{

	public function run()
	{
		Yii::app()->clientScript->registerCoreScript('jquery.ui');
	 	$baseUrl = Yii::app()->baseUrl; 
	 	Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/modules/admin/components/'.__CLASS__.'/jquery.Treetable.js');
	 	Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/modules/admin/components/'.__CLASS__.'/jquery.CMenuLink.js');
	  	Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/modules/admin/components/'.__CLASS__.'/jquery.Treetable.css');
		echo  '<table id="dnd-example" style="width:100%" class="ui-sortable">
		  <thead>
		    <tr>
		      <th>Menu title</th>
		      <th>Menu path</th>
		    </tr>
		  </thead>
		  <tbody>';
	  	echo $this->renderHierachy('admin');
	  	echo '</tbody>
		</table>';
	}
	/**
	 * This method return HTML for the tree
	 * Enter description here ...
	 * @param unknown_type $parent
	 * @param unknown_type $menuData
	 */
	public function buildTree($parent, $menuData)
	{
		$html = "";
	
		if (isset($menuData['parents'][$parent]))
	    {
	        foreach ($menuData['parents'][$parent] as $parentId)
	        {
	            //die(print_r($menuData['items'][$parentName]));
	        	
	        	//$html .= '<li id="'.$parent.'_'.$menuData['items'][$parentName]->name.'" class="tree_item '.$this->types[$menuData['items'][$parentName]->type].'"><a href="#"><span>' . $menuData['items'][$parentName]->name.'</span></a>';
	        	
	        	$class = " ui-droppable ";
	        	if(isset($menuData['parents'][$menuData['items'][$parentId]->link_id])){
	        	$class .= " parent";
	        	}
	        	
	        	if($menuData['items'][$parentId]->parent > 0){
	        	$class .= " child-of-node--".$menuData['items'][$parentId]->parent;
	        	}
        		$html .= '<tr id="node--'.$menuData['items'][$parentId]->link_id.'" class="'.$class.'"><td><span class="menu_item ui-draggable">'.$menuData['items'][$parentId]->link_title.'</span></td><td>'.$menuData['items'][$parentId]->link_path.'</td></tr>';
	            // find childitems recursively
	            $html .= $this->buildTree($parentId, $menuData);
	        }
	    }

	    return $html;
	}
	
	/**
	 * This method collect data from authManager and render the tree
	 * Enter description here ...
	 * @param unknown_type $root
	 */
	public function renderHierachy($menu){
		Yii::import('application.modules.default.models.Menu');
		Yii::import('application.modules.default.models.MenuLink');		
				
		$data = MenuLink::model()->findAll("menu_name=:menu_name",array(":menu_name"=>$menu));
		//die(print_r($data));
		$menuData = array(
		    'items' => array(),
    		'parents' => array()
		);
		foreach($data as $menuItem){
			$menuData['items'][$menuItem['link_id']] = $menuItem;
	    	$menuData['parents'][$menuItem['parent']][] = $menuItem['link_id']; 
		}
		//die(print_r($menuData));
		return $this->buildTree('0', $menuData);
	}
	
}